//
//  BLHotleOrderViewController.m
//  blanket
//
//  Created by Mac on 2018/11/15.
//  Copyright © 2018 stoneobs@icloud.com. All rights reserved.
//

#import "BLHotleOrderViewController.h"
#import "BlHotleOrderTableViewCell.h"
#import "BLHotlePayViewController.h"
@interface BLHotleOrderViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;/**<  */
@end

@implementation BLHotleOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的订单";
    __weak typeof(self) weakSelf =  self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf sendMyListRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    // Do any additional setup after loading the view.
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return BlHotleOrderTableViewCell.cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    BlHotleOrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[BlHotleOrderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    BLHotleOrderModel * model = self.dataSouce[indexPath.section];
    cell.model = model;
    __weak typeof(self) weakSelf =  self;
    [cell.menuButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedMenuTitle:sender.currentTitle indexPath:indexPath];
    }];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSelctedMenuTitle:(NSString*)title indexPath:(NSIndexPath*)indexPath{
    BLHotleOrderModel * model = self.dataSouce[indexPath.section];
    if ([title isEqualToString:@"付款"]) {
        BLHotlePayViewController * vc = BLHotlePayViewController.new;
        vc.orderNum = model.order_num;
        vc.moneySting = [NSString stringWithFormat:@"￥%0.2f",model.price.floatValue * model.num.integerValue];
        vc.desString = [NSString stringWithFormat:@"描述:%@间,%@",model.num,model.room_name];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if ([title isEqualToString:@"联系酒店"]) {
        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",model.phone]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

    }
}
#pragma mark --NetWork Method
- (void)sendMyListRequest{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"/api/hotel/myOrder/p/1"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 NSArray * array = responseObject[@"data"];
                                                 self.dataSouce = NSMutableArray.new;
                                                 for (NSDictionary * modelDIc in array) {
                                                     BLHotleOrderModel * model = [BLHotleOrderModel mj_objectWithKeyValues:modelDIc];
                                                     [self.dataSouce addObject:model];
                                                 }
                                                 [self.tableView reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}
@end
